#include "usersql.h"
#include "ui_usersql.h"
#include "mainwindow.h"

QString id, name, sex, idcard, address, phone;
QString IID = "";
//QString visit_time, first_symptom, elapsed_time, h, weight, smoking_history, previous_illnesses, comorbidity, thymic_disease, myasthenia, antibody_test, blood_test, technical_inspection, scale_evaluation1, scale_evaluation2, diagnosis1, diagnosis2, treatment_plan, MGFA;
//int age = 0;

userSql::userSql(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::userSql)
{
    ui->setupUi(this);
    ui->treeWidget->clear();
    ui->treeWidget->setColumnCount(1);
    QStringList list;
    list << "患者信息管理";
    //ui->treeWidget->addTopLevelItem(new QTreeWidgetItem(ui->treeWidget, list));

    QTreeWidgetItem *pf = new QTreeWidgetItem(ui->treeWidget, list);
    ui->treeWidget->addTopLevelItem(pf);

    list.clear();
    list << "基本信息";
    QTreeWidgetItem *p1 = new QTreeWidgetItem(pf, list);
    list.clear();
    list << "诊断结果";
    QTreeWidgetItem *p2 = new QTreeWidgetItem(pf, list);

    pf->addChild(p1);
    pf->addChild(p2);

    ui->treeWidget->expandAll();

    ui->textEdit->clear();
    ui->label_image->clear();

//    db=QSqlDatabase::addDatabase("QSQLITE");
//    db.setDatabaseName("D:/test.db");
//    db.open();
//    qmodel = new QSqlQueryModel();
//    qmodel->setQuery("select * from patient_detail_info");
//    ui->tableWidget->setModel(qmodel);
//    qmodel->setHeaderData(0, Qt::Horizontal, "id");
}

userSql::load(QList<Info> list)
{
    ui->tableWidget2->setRowCount(list.size());
//    connect(button, SIGNAL(clicked()), this, SLOT(OnBtnClicked()));
    int k = 2;
    for(int i = 0; i < list.size(); i++)
    {
        ui->tableWidget2->setItem(i, 0, new QTableWidgetItem("第" + QString::number(k++) + "次就诊"));
        ui->tableWidget2->setItem(i, 1, new QTableWidgetItem(list[i].visit_time));
        ui->tableWidget2->setItem(i, 2, new QTableWidgetItem(list[i].first_symptom));
        ui->tableWidget2->setItem(i, 3, new QTableWidgetItem(list[i].height));
        ui->tableWidget2->setItem(i, 4, new QTableWidgetItem(list[i].weight));
        ui->tableWidget2->setItem(i, 5, new QTableWidgetItem(list[i].comorbidity));
        ui->tableWidget2->setItem(i, 6, new QTableWidgetItem(list[i].thymic_disease));
        ui->tableWidget2->setItem(i, 7, new QTableWidgetItem(list[i].myasthenia));
//        ui->tableWidget2->setItem(i, 8, new QTableWidgetItem(list[i].antibody_test));

        button = new QPushButton();
        button->setText("点击查看");
        button->adjustSize();
//        button->setFixedSize(80, 30);
        button->setStyleSheet("background-color:white");
        connect(button, SIGNAL(clicked(bool)), this, SLOT(clickButton_antibody1()));
        ui->tableWidget2->setCellWidget(i, 8, button);

//        ui->tableWidget2->setItem(i, 9, new QTableWidgetItem(list[i].blood_test));

        button = new QPushButton();
        button->setText("点击查看");
        button->adjustSize();
//        button->setFixedSize(80, 30);
        button->setStyleSheet("background-color:white");
        connect(button, SIGNAL(clicked(bool)), this, SLOT(clickButton_blood1()));
        ui->tableWidget2->setCellWidget(i, 9, button);

//        ui->tableWidget2->setItem(i, 10, new QTableWidgetItem(list[i].technical_inspection));

        button = new QPushButton();
        button->setText("点击查看");
        button->adjustSize();
//        button->setFixedSize(80, 30);
        button->setStyleSheet("background-color:white");
        connect(button, SIGNAL(clicked(bool)), this, SLOT(clickButton_tec1()));
        ui->tableWidget2->setCellWidget(i, 10, button);

//        ui->tableWidget2->setItem(i, 11, new QTableWidgetItem(list[i].scale_evaluation1));

        button = new QPushButton();
        button->setText("点击查看");
        button->adjustSize();
//        button->setFixedSize(80, 30);
        button->setStyleSheet("background-color:white");
        connect(button, SIGNAL(clicked(bool)), this, SLOT(clickButton1()));
        ui->tableWidget2->setCellWidget(i, 11, button);

//        ui->tableWidget2->setItem(i, 12, new QTableWidgetItem(list[i].diagnosis1));

        button = new QPushButton();
        button->setText("点击查看");
        button->adjustSize();
//        button->setFixedSize(80, 30);
        button->setStyleSheet("background-color:white");
        connect(button, SIGNAL(clicked(bool)), this, SLOT(clickButton_diagnosis1()));
        ui->tableWidget2->setCellWidget(i, 12, button);

        ui->tableWidget2->setItem(i, 13, new QTableWidgetItem(list[i].treatment_plan));
        ui->tableWidget2->setItem(i, 14, new QTableWidgetItem(list[i].MGFA));
        ui->tableWidget2->setItem(i, 15, new QTableWidgetItem(QString::number(list[i].id)));
        qDebug() << list[i].id;
        for(int j = 0; j <= 15; j++)
        {
            if(j == 8 || j == 9 || j == 10 || j == 11 || j == 12) continue;
            ui->tableWidget2->item(i, j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        }
    }
}

userSql::load(Info info)
{
    if(QString::compare(info.visit_time, "") == 0 && QString::compare(info.first_symptom, "") == 0 && QString::compare(info.elapsed_time, "") == 0
            && QString::compare(info.height, "") == 0 && QString::compare(info.weight, "") == 0 && QString::compare(info.smoking_history, "") == 0
            && QString::compare(info.previous_illnesses, "") == 0 && QString::compare(info.comorbidity, "") == 0 && QString::compare(info.thymic_disease, "") == 0
            && QString::compare(info.myasthenia, "") == 0 && QString::compare(info.antibody_test, "") == 0 && QString::compare(info.blood_test, "") == 0
            && QString::compare(info.technical_inspection, "") == 0 && QString::compare(info.scale_evaluation1, "") == 0 && QString::compare(info.scale_evaluation2, "") == 0
            && QString::compare(info.diagnosis1, "") == 0 && QString::compare(info.diagnosis2, "") == 0 && QString::compare(info.treatment_plan, "") == 0 && QString::compare(info.MGFA, "") == 0
            && info.age == 0)
    {
        ui->tableWidget1->setRowCount(0);
    }
    else
    {
//        visit_time = info.visit_time;
//        first_symptom = info.first_symptom;
//        elapsed_time = info.elapsed_time;
//        h = info.height;
//        weight = info.weight;
//        smoking_history = info.smoking_history;
//        previous_illnesses = info.previous_illnesses;
//        comorbidity = info.comorbidity;
//        thymic_disease = info.thymic_disease;
//        myasthenia = info.myasthenia;
//        antibody_test = info.antibody_test;
//        blood_test = info.blood_test;
//        technical_inspection = info.technical_inspection;
//        scale_evaluation1 = info.scale_evaluation1;
//        scale_evaluation2 = info.scale_evaluation2;
//        diagnosis1 = info.diagnosis1;
//        diagnosis2 = info.diagnosis2;
//        treatment_plan = info.treatment_plan;
//        MGFA = info.MGFA;
//        age = info.age;

        ui->tableWidget1->setRowCount(1);
        ui->tableWidget1->setItem(0, 0, new QTableWidgetItem("第1次就诊"));
        ui->tableWidget1->setItem(0, 1, new QTableWidgetItem(info.visit_time));
        ui->tableWidget1->setItem(0, 2, new QTableWidgetItem(info.first_symptom));
        ui->tableWidget1->setItem(0, 3, new QTableWidgetItem(QString::number(info.age)));
        ui->tableWidget1->setItem(0, 4, new QTableWidgetItem(info.elapsed_time));
        ui->tableWidget1->setItem(0, 5, new QTableWidgetItem(info.height));
        ui->tableWidget1->setItem(0, 6, new QTableWidgetItem(info.weight));
        ui->tableWidget1->setItem(0, 7, new QTableWidgetItem(info.smoking_history));
        ui->tableWidget1->setItem(0, 8, new QTableWidgetItem(info.previous_illnesses));
        ui->tableWidget1->setItem(0, 9, new QTableWidgetItem(info.comorbidity));
        ui->tableWidget1->setItem(0, 10, new QTableWidgetItem(info.thymic_disease));
        ui->tableWidget1->setItem(0, 11, new QTableWidgetItem(info.myasthenia));
//        ui->tableWidget1->setItem(0, 12, new QTableWidgetItem(info.antibody_test));

        button = new QPushButton();
        button->setText("点击查看");
        button->adjustSize();
//        button->setFixedSize(80, 30);
        button->setStyleSheet("background-color:white");
        connect(button, SIGNAL(clicked(bool)), this, SLOT(clickButton_antibody2()));
        ui->tableWidget1->setCellWidget(0, 12, button);

//        ui->tableWidget1->setItem(0, 13, new QTableWidgetItem(info.blood_test));

        button = new QPushButton();
        button->setText("点击查看");
        button->adjustSize();
//        button->setFixedSize(80, 30);
        button->setStyleSheet("background-color:white");
        connect(button, SIGNAL(clicked(bool)), this, SLOT(clickButton_blood2()));
        ui->tableWidget1->setCellWidget(0, 13, button);

//        ui->tableWidget1->setItem(0, 14, new QTableWidgetItem(info.technical_inspection));

        button = new QPushButton();
        button->setText("点击查看");
        button->adjustSize();
//        button->setFixedSize(80, 30);
        button->setStyleSheet("background-color:white");
        connect(button, SIGNAL(clicked(bool)), this, SLOT(clickButton_tec2()));
        ui->tableWidget1->setCellWidget(0, 14, button);

//        ui->tableWidget1->setItem(0, 15, new QTableWidgetItem(info.scale_evaluation1));

        button = new QPushButton();
        button->setText("点击查看");
        button->adjustSize();
//        button->setFixedSize(80, 30);
        button->setStyleSheet("background-color:white");
        connect(button, SIGNAL(clicked(bool)), this, SLOT(clickButton2()));
        ui->tableWidget1->setCellWidget(0, 15, button);

//        ui->tableWidget1->setItem(0, 16, new QTableWidgetItem(info.diagnosis1));

        button = new QPushButton();
        button->setText("点击查看");
        button->adjustSize();
//        button->setFixedSize(80, 30);
        button->setStyleSheet("background-color:white");
        connect(button, SIGNAL(clicked(bool)), this, SLOT(clickButton_diagnosis2()));
        ui->tableWidget1->setCellWidget(0, 16, button);

        ui->tableWidget1->setItem(0, 17, new QTableWidgetItem(info.treatment_plan));
        ui->tableWidget1->setItem(0, 18, new QTableWidgetItem(info.MGFA));
        for(int i = 0; i <= 18; i++)
        {
            if(i == 12 || i == 13 || i == 14 || i == 15 || i == 16) continue;
            ui->tableWidget1->item(0, i)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        }
    }
}

userSql::load(User user)
{
    ui->textEdit->clear();
    ui->label_image->clear();

    qDebug() << "load...";
    ui->lb_id->clear();
    ui->lb_name->clear();
    ui->lb_sex->clear();
    ui->lb_idcard->clear();
    ui->lb_address->clear();
    ui->lb_phone->clear();
    id = QString::number(user.id);
    name = user.name;
    sex = user.sex;
    idcard = user.idcard;
    address = user.address;
    phone = user.phone;
    ui->lb_id->setText(id);
    ui->lb_name->setText(name);
    ui->lb_sex->setText(sex);
    ui->lb_idcard->setText(idcard);
    ui->lb_address->setText(address);
    ui->lb_phone->setText(phone);
    ui->stackedWidget->setCurrentWidget(ui->page_baseData);
}

userSql::~userSql()
{
    delete ui;
}

void userSql::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
{
    if(item->text(column) == "基本信息")
    {
        ui->stackedWidget->setCurrentWidget(ui->page_baseData);
    }
    if(item->text(column) == "诊断结果")
    {
        ui->stackedWidget->setCurrentWidget(ui->page_detailData);
    }
}

void userSql::on_btn_mod_clicked()
{
    mod_User.load(id, name, sex, idcard, address, phone);
    mod_User.exec();
    User user;
    user.id = id.toInt();
    user.name = name;
    user.sex = sex;
    user.idcard = idcard;
    user.address = address;
    user.phone = phone;
    load(user);
}

void userSql::on_btn_exit_clicked()
{
    this->hide();
    ui->stackedWidget->setCurrentWidget(ui->page_baseData);
}

//void userSql::on_tableWidget_clicked(const QModelIndex &index)
//{
//    ui->textEdit->setText(index.data().toString());
//}


void userSql::clickButton1()
{
    ui->textEdit->clear();
    ui->label_image->clear();

    QPushButton *pushButton_ = dynamic_cast<QPushButton*>(this->sender());
    if(NULL == pushButton_)
    {
        return;
    }
    int x = pushButton_->frameGeometry().x();
    int y = pushButton_->frameGeometry().y();
    QModelIndex index = ui->tableWidget2->indexAt(QPoint(x, y));

    int row = index.row();
    int column = index.column();

    qDebug() << "选择第几行:" << row << "选择第几列:" << column;

    //接下来自定义要处理的
    QString ID = index.sibling(row, 15).data().toString();
    IID = ID;
    auto ptr = MainWindow::getinstance();
    Info info = ptr->getInfoById(ID.toInt());
    ui->textEdit->setText(info.scale_evaluation1);
    QImage image;
    QString path = info.scale_evaluation2;
    qDebug() << path;
    image.load(path);
    QPixmap pixmap = QPixmap::fromImage(image);
    int width = ui->label_image->width();
    int height = ui->label_image->height();
    QPixmap fitpixmap = pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->label_image->setPixmap(fitpixmap);
    ptr = nullptr;
}

void userSql::clickButton2()
{
    ui->textEdit->clear();
    ui->label_image->clear();

    QPushButton *pushButton_ = dynamic_cast<QPushButton*>(this->sender());
    if(NULL == pushButton_)
    {
        return;
    }
    int x = pushButton_->frameGeometry().x();
    int y = pushButton_->frameGeometry().y();
    QModelIndex index = ui->tableWidget1->indexAt(QPoint(x, y));

    int row = index.row();
    int column = index.column();

    qDebug() << "选择第几行:" << row << "选择第几列:" << column;

    //接下来自定义要处理的
    auto ptr = MainWindow::getinstance();
    Info info = ptr->getFirstInfoById(id.toInt());
    ui->textEdit->setText(info.scale_evaluation1);
    QImage image;
    QString path = info.scale_evaluation2;
    qDebug() << path;
    image.load(path);
    QPixmap pixmap = QPixmap::fromImage(image);
    int width = ui->label_image->width();
    int height = ui->label_image->height();
    QPixmap fitpixmap = pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->label_image->setPixmap(fitpixmap);
    ptr = nullptr;
}

void userSql::clickButton_tec1()
{
    ui->textEdit->clear();
    ui->label_image->clear();

    QPushButton *pushButton_ = dynamic_cast<QPushButton*>(this->sender());
    if(NULL == pushButton_)
    {
        return;
    }
    int x = pushButton_->frameGeometry().x();
    int y = pushButton_->frameGeometry().y();
    QModelIndex index = ui->tableWidget2->indexAt(QPoint(x, y));

    int row = index.row();
    int column = index.column();

    qDebug() << "选择第几行:" << row << "选择第几列:" << column;

    //接下来自定义要处理的
    QString ID = index.sibling(row, 15).data().toString();
    IID = ID;
    auto ptr = MainWindow::getinstance();
    Info info = ptr->getInfoById(ID.toInt());
    QImage image;
    QString path = info.technical_inspection;
    qDebug() << path;
    image.load(path);
    QPixmap pixmap = QPixmap::fromImage(image);
    int width = ui->label_image->width();
    int height = ui->label_image->height();
    QPixmap fitpixmap = pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->label_image->setPixmap(fitpixmap);
    ptr = nullptr;
}

void userSql::clickButton_tec2()
{
    ui->textEdit->clear();
    ui->label_image->clear();

    QPushButton *pushButton_ = dynamic_cast<QPushButton*>(this->sender());
    if(NULL == pushButton_)
    {
        return;
    }
    int x = pushButton_->frameGeometry().x();
    int y = pushButton_->frameGeometry().y();
    QModelIndex index = ui->tableWidget1->indexAt(QPoint(x, y));

    int row = index.row();
    int column = index.column();

    qDebug() << "选择第几行:" << row << "选择第几列:" << column;

    //接下来自定义要处理的
    auto ptr = MainWindow::getinstance();
    Info info = ptr->getFirstInfoById(id.toInt());
    QImage image;
    QString path = info.technical_inspection;
    qDebug() << path;
    image.load(path);
    QPixmap pixmap = QPixmap::fromImage(image);
    int width = ui->label_image->width();
    int height = ui->label_image->height();
    QPixmap fitpixmap = pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->label_image->setPixmap(fitpixmap);
    ptr = nullptr;
}

void userSql::clickButton_blood1()
{
    ui->textEdit->clear();
    ui->label_image->clear();

    QPushButton *pushButton_ = dynamic_cast<QPushButton*>(this->sender());
    if(NULL == pushButton_)
    {
        return;
    }
    int x = pushButton_->frameGeometry().x();
    int y = pushButton_->frameGeometry().y();
    QModelIndex index = ui->tableWidget2->indexAt(QPoint(x, y));

    int row = index.row();
    int column = index.column();

    qDebug() << "选择第几行:" << row << "选择第几列:" << column;

    //接下来自定义要处理的
    QString ID = index.sibling(row, 15).data().toString();
    IID = ID;
    auto ptr = MainWindow::getinstance();
    Info info = ptr->getInfoById(ID.toInt());
    QImage image;
    QString path = info.blood_test;
    qDebug() << path;
    image.load(path);
    QPixmap pixmap = QPixmap::fromImage(image);
    int width = ui->label_image->width();
    int height = ui->label_image->height();
    QPixmap fitpixmap = pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->label_image->setPixmap(fitpixmap);
    ptr = nullptr;
}

void userSql::clickButton_blood2()
{
    ui->textEdit->clear();
    ui->label_image->clear();

    QPushButton *pushButton_ = dynamic_cast<QPushButton*>(this->sender());
    if(NULL == pushButton_)
    {
        return;
    }
    int x = pushButton_->frameGeometry().x();
    int y = pushButton_->frameGeometry().y();
    QModelIndex index = ui->tableWidget1->indexAt(QPoint(x, y));

    int row = index.row();
    int column = index.column();

    qDebug() << "选择第几行:" << row << "选择第几列:" << column;

    //接下来自定义要处理的
    auto ptr = MainWindow::getinstance();
    Info info = ptr->getFirstInfoById(id.toInt());
    QImage image;
    QString path = info.blood_test;
    qDebug() << path;
    image.load(path);
    QPixmap pixmap = QPixmap::fromImage(image);
    int width = ui->label_image->width();
    int height = ui->label_image->height();
    QPixmap fitpixmap = pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->label_image->setPixmap(fitpixmap);
    ptr = nullptr;
}

void userSql::clickButton_antibody1()
{
    ui->textEdit->clear();
    ui->label_image->clear();

    QPushButton *pushButton_ = dynamic_cast<QPushButton*>(this->sender());
    if(NULL == pushButton_)
    {
        return;
    }
    int x = pushButton_->frameGeometry().x();
    int y = pushButton_->frameGeometry().y();
    QModelIndex index = ui->tableWidget2->indexAt(QPoint(x, y));

    int row = index.row();
    int column = index.column();

    qDebug() << "选择第几行:" << row << "选择第几列:" << column;

    //接下来自定义要处理的
    QString ID = index.sibling(row, 15).data().toString();
    IID = ID;
    auto ptr = MainWindow::getinstance();
    Info info = ptr->getInfoById(ID.toInt());
    QImage image;
    QString path = info.antibody_test;
    qDebug() << path;
    image.load(path);
    QPixmap pixmap = QPixmap::fromImage(image);
    int width = ui->label_image->width();
    int height = ui->label_image->height();
    QPixmap fitpixmap = pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->label_image->setPixmap(fitpixmap);
    ptr = nullptr;
}

void userSql::clickButton_antibody2()
{
    ui->textEdit->clear();
    ui->label_image->clear();

    QPushButton *pushButton_ = dynamic_cast<QPushButton*>(this->sender());
    if(NULL == pushButton_)
    {
        return;
    }
    int x = pushButton_->frameGeometry().x();
    int y = pushButton_->frameGeometry().y();
    QModelIndex index = ui->tableWidget1->indexAt(QPoint(x, y));

    int row = index.row();
    int column = index.column();

    qDebug() << "选择第几行:" << row << "选择第几列:" << column;

    //接下来自定义要处理的
    auto ptr = MainWindow::getinstance();
    Info info = ptr->getFirstInfoById(id.toInt());
    QImage image;
    QString path = info.antibody_test;
    qDebug() << path;
    image.load(path);
    QPixmap pixmap = QPixmap::fromImage(image);
    int width = ui->label_image->width();
    int height = ui->label_image->height();
    QPixmap fitpixmap = pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->label_image->setPixmap(fitpixmap);
    ptr = nullptr;
}

void userSql::clickButton_diagnosis1()
{
    ui->textEdit->clear();
    ui->label_image->clear();

    QPushButton *pushButton_ = dynamic_cast<QPushButton*>(this->sender());
    if(NULL == pushButton_)
    {
        return;
    }
    int x = pushButton_->frameGeometry().x();
    int y = pushButton_->frameGeometry().y();
    QModelIndex index = ui->tableWidget2->indexAt(QPoint(x, y));

    int row = index.row();
    int column = index.column();

    qDebug() << "选择第几行:" << row << "选择第几列:" << column;

    //接下来自定义要处理的
    QString ID = index.sibling(row, 15).data().toString();
    IID = ID;
    auto ptr = MainWindow::getinstance();
    Info info = ptr->getInfoById(ID.toInt());
    ui->textEdit->setText(info.diagnosis1);
    QImage image;
    QString path = info.diagnosis2;
    qDebug() << path;
    image.load(path);
    QPixmap pixmap = QPixmap::fromImage(image);
    int width = ui->label_image->width();
    int height = ui->label_image->height();
    QPixmap fitpixmap = pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->label_image->setPixmap(fitpixmap);
    ptr = nullptr;
}

void userSql::clickButton_diagnosis2()
{
    ui->textEdit->clear();
    ui->label_image->clear();

    QPushButton *pushButton_ = dynamic_cast<QPushButton*>(this->sender());
    if(NULL == pushButton_)
    {
        return;
    }
    int x = pushButton_->frameGeometry().x();
    int y = pushButton_->frameGeometry().y();
    QModelIndex index = ui->tableWidget1->indexAt(QPoint(x, y));

    int row = index.row();
    int column = index.column();

    qDebug() << "选择第几行:" << row << "选择第几列:" << column;

    //接下来自定义要处理的
    auto ptr = MainWindow::getinstance();
    Info info = ptr->getFirstInfoById(id.toInt());
    ui->textEdit->setText(info.diagnosis1);
    QImage image;
    QString path = info.diagnosis2;
    qDebug() << path;
    image.load(path);
    QPixmap pixmap = QPixmap::fromImage(image);
    int width = ui->label_image->width();
    int height = ui->label_image->height();
    QPixmap fitpixmap = pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->label_image->setPixmap(fitpixmap);
    ptr = nullptr;
}

void userSql::on_btn_remove_clicked()
{
    auto ptr = MainWindow::getinstance();
    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(this, tr("Question"), tr("Do you want to delete this user?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
    if(reply == QMessageBox::Yes)
    {
        ptr->delUser(id.toInt());
        this->hide();
    }
    ptr = nullptr;
}

void userSql::on_tableWidget1_clicked(const QModelIndex &index)
{
    ui->textEdit->clear();
    ui->label_image->clear();
    ui->textEdit->setText(index.data().toString());
    IID = index.sibling(index.row(), 15).data().toString();
}

void userSql::on_tableWidget2_clicked(const QModelIndex &index)
{
    ui->textEdit->clear();
    ui->label_image->clear();
    ui->textEdit->setText(index.data().toString());
    IID = index.sibling(index.row(), 15).data().toString();
}

void userSql::on_btn_add_info_clicked()
{
    auto ptr = MainWindow::getinstance();
    if(ptr->isExistFirstInfo(id))
    {
        ai.setWindowFlags(af.windowFlags() | Qt::Dialog);
        ai.setWindowModality(Qt::ApplicationModal);

        ai.showMaximized();
    }
    else
    {
        QMessageBox::warning(this, tr("Warning"), tr("Operation failed: Please fill in the first visit information first!"), QMessageBox::Yes);
    }

    ptr = nullptr;
}

void userSql::on_btn_del_info_clicked()
{
    qDebug() << IID;
    if(QString::compare(IID, "") == 0)
    {
        QMessageBox::warning(this, tr("Warning"), tr("Operation failed: Please select first!"), QMessageBox::Yes);
        return;
    }
    auto ptr = MainWindow::getinstance();
    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(this, tr("Question"), tr("Do you want to delete this information?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
    if(reply == QMessageBox::Yes)
    {
        ptr->delInfoById(IID);
        QList<Info> list = ptr->getAllInfoById(id.toInt());
        load(list);
    }
    ptr = nullptr;
}

void userSql::on_btn_add_first_info_clicked()
{
    auto ptr = MainWindow::getinstance();
    if(ptr->isExistFirstInfo(id))
    {
        QMessageBox::warning(this, tr("Warning"), tr("Operation failed: First visit information already available!"), QMessageBox::Yes);
    }
    else
    {
        af.setWindowFlags(af.windowFlags() | Qt::Dialog);
        af.setWindowModality(Qt::ApplicationModal);

        af.showMaximized();

//        connect(af,&af->Tx_NewSig,this,&MainWindow::Rx_NewSig);

//        af.setAttribute(QtCore.Qt.WA_DeleteOnClose);

        Info info = ptr->getFirstInfoById(id.toInt());
//        Info info;
//        info.visit_time = visit_time;
//        info.first_symptom = first_symptom;
//        info.age = age;
//        info.elapsed_time = elapsed_time;
//        info.height = h;
//        info.weight = weight;
//        info.smoking_history = smoking_history;
//        info.previous_illnesses = previous_illnesses;
//        info.comorbidity = comorbidity;
//        info.thymic_disease = thymic_disease;
//        info.myasthenia = myasthenia;
//        info.antibody_test = antibody_test;
//        info.blood_test = blood_test;
//        info.technical_inspection = technical_inspection;
//        info.scale_evaluation1 = scale_evaluation1;
//        info.scale_evaluation2 = scale_evaluation2;
//        info.diagnosis1 = diagnosis1;
//        info.treatment_plan = treatment_plan;
//        info.MGFA = MGFA;
        load(info);
    }
    ptr = nullptr;
}

QString userSql::getUserSqlId()
{
    return id;
}

void userSql::on_btn_update_clicked()
{
    auto ptr = MainWindow::getinstance();
    Info info = ptr->getFirstInfoById(id.toInt());
    load(info);
    QList<Info> list = ptr->getAllInfoById(id.toInt());
    load(list);
    ptr = nullptr;
}

void userSql::on_btn_firstmod_clicked()
{
    auto ptr = MainWindow::getinstance();
    if(ptr->isExistFirstInfo(id))
    {
//      ptr->delInfoById(IID);
        Info info = ptr->getFirstInfoById(id.toInt());
        if(QString::compare(info.previous_illnesses, "无") == 0)
        {
            info.previous_illnesses = "";
        }
        if(QString::compare(info.comorbidity, "无") == 0)
        {
            info.comorbidity = "";
        }
        if(QString::compare(info.antibody_test, "无") == 0)
        {
            info.antibody_test = "";
        }
        if(QString::compare(info.blood_test, "无") == 0)
        {
            info.blood_test = "";
        }
        if(QString::compare(info.technical_inspection, "无") == 0)
        {
            info.technical_inspection = "";
        }
        if(QString::compare(info.thymic_disease, "未查") == 0)
        {
            info.thymic_disease = "";
        }
        if(QString::compare(info.myasthenia, "无") == 0)
        {
            info.myasthenia = "";
        }
        if(QString::compare(info.MGFA, "无干预") == 0)
        {
            info.MGFA = "";
        }
        mfi.load(info.visit_time, info.first_symptom, info.age, info.elapsed_time, info.height, info.weight, info.smoking_history, info.previous_illnesses, info.comorbidity, info.thymic_disease, info.myasthenia, info.antibody_test, info.blood_test, info.technical_inspection, info.scale_evaluation1, info.scale_evaluation2, info.diagnosis1, info.diagnosis2, info.treatment_plan, info.MGFA);
        mfi.setWindowFlags(af.windowFlags() | Qt::Dialog);
        mfi.setWindowModality(Qt::ApplicationModal);
        mfi.showMaximized();

        QList<Info> list = ptr->getAllInfoById(id.toInt());
        load(list);
    }
    ptr = nullptr;
}

void userSql::on_btn_mod_2_clicked()
{
    if(QString::compare(IID, "") == 0)
    {
        QMessageBox::warning(this, tr("Warning"), tr("Operation failed: Please select first!"), QMessageBox::Yes);
        return;
    }
    auto ptr = MainWindow::getinstance();
//    ptr->delInfoById(IID);
//    Info info = ptr->getFirstInfoById(id.toInt());
    Info info = ptr->getInfoById(IID.toInt());
    if(QString::compare(info.comorbidity, "无") == 0)
    {
        info.comorbidity = "";
    }
    if(QString::compare(info.antibody_test, "无") == 0)
    {
        info.antibody_test = "";
    }
    if(QString::compare(info.blood_test, "无") == 0)
    {
        info.blood_test = "";
    }
    if(QString::compare(info.technical_inspection, "无") == 0)
    {
        info.technical_inspection = "";
    }
    if(QString::compare(info.thymic_disease, "未查") == 0)
    {
        info.thymic_disease = "";
    }
    if(QString::compare(info.myasthenia, "无") == 0)
    {
        info.myasthenia = "";
    }
    if(QString::compare(info.MGFA, "无干预") == 0)
    {
        info.MGFA = "";
    }
    mi.load(IID, info.visit_time, info.first_symptom, info.height, info.weight, info.comorbidity, info.thymic_disease, info.myasthenia, info.antibody_test, info.blood_test, info.technical_inspection, info.scale_evaluation1, info.scale_evaluation2, info.diagnosis1, info.diagnosis2, info.treatment_plan, info.MGFA);
    mi.setWindowFlags(af.windowFlags() | Qt::Dialog);
    mi.setWindowModality(Qt::ApplicationModal);
    mi.showMaximized();

    QList<Info> list = ptr->getAllInfoById(id.toInt());
    load(list);
    ptr = nullptr;
}

void userSql::on_btn_view_first_info_clicked()
{
    auto ptr = MainWindow::getinstance();
    if(ptr->isExistFirstInfo(id))
    {
//      ptr->delInfoById(IID);
        Info info = ptr->getFirstInfoById(id.toInt());
        if(QString::compare(info.previous_illnesses, "无") == 0)
        {
            info.previous_illnesses = "";
        }
        if(QString::compare(info.comorbidity, "无") == 0)
        {
            info.comorbidity = "";
        }
        if(QString::compare(info.antibody_test, "无") == 0)
        {
            info.antibody_test = "";
        }
        if(QString::compare(info.blood_test, "无") == 0)
        {
            info.blood_test = "";
        }
        if(QString::compare(info.technical_inspection, "无") == 0)
        {
            info.technical_inspection = "";
        }
        if(QString::compare(info.thymic_disease, "未查") == 0)
        {
            info.thymic_disease = "";
        }
        if(QString::compare(info.myasthenia, "无") == 0)
        {
            info.myasthenia = "";
        }
        if(QString::compare(info.MGFA, "无干预") == 0)
        {
            info.MGFA = "";
        }
        vfi.load(info.visit_time, info.first_symptom, info.age, info.elapsed_time, info.height, info.weight, info.smoking_history, info.previous_illnesses, info.comorbidity, info.thymic_disease, info.myasthenia, info.antibody_test, info.blood_test, info.technical_inspection, info.scale_evaluation1, info.scale_evaluation2, info.diagnosis1, info.diagnosis2, info.treatment_plan, info.MGFA);
        vfi.setWindowFlags(af.windowFlags() | Qt::Dialog);
        vfi.setWindowModality(Qt::ApplicationModal);
        vfi.showMaximized();
    }
    ptr = nullptr;
}

void userSql::on_btn_view_info_clicked()
{
    if(QString::compare(IID, "") == 0)
    {
        QMessageBox::warning(this, tr("Warning"), tr("Operation failed: Please select first!"), QMessageBox::Yes);
        return;
    }
    auto ptr = MainWindow::getinstance();
    Info info = ptr->getInfoById(IID.toInt());
    if(QString::compare(info.comorbidity, "无") == 0)
    {
        info.comorbidity = "";
    }
    if(QString::compare(info.antibody_test, "无") == 0)
    {
        info.antibody_test = "";
    }
    if(QString::compare(info.blood_test, "无") == 0)
    {
        info.blood_test = "";
    }
    if(QString::compare(info.technical_inspection, "无") == 0)
    {
        info.technical_inspection = "";
    }
    if(QString::compare(info.thymic_disease, "未查") == 0)
    {
        info.thymic_disease = "";
    }
    if(QString::compare(info.myasthenia, "无") == 0)
    {
        info.myasthenia = "";
    }
    if(QString::compare(info.MGFA, "无干预") == 0)
    {
        info.MGFA = "";
    }
    vi.load(info.visit_time, info.first_symptom, info.height, info.weight, info.comorbidity, info.thymic_disease, info.myasthenia, info.antibody_test, info.blood_test, info.technical_inspection, info.scale_evaluation1, info.scale_evaluation2, info.diagnosis1, info.diagnosis2, info.treatment_plan, info.MGFA);
    vi.setWindowFlags(af.windowFlags() | Qt::Dialog);
    vi.setWindowModality(Qt::ApplicationModal);
    vi.showMaximized();

    ptr = nullptr;
}
